package com.ym.config;

import com.github.xiaoymin.knife4j.core.enums.OpenAPILanguageEnums;
import com.github.xiaoymin.knife4j.spring.configuration.Knife4jHttpBasic;
import com.github.xiaoymin.knife4j.spring.configuration.Knife4jProperties;
import com.github.xiaoymin.knife4j.spring.configuration.Knife4jSetting;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Knife4jAutoConfig {

    @Value("${spring.application.name}")
    private String serviceName;

    @Bean
    public OpenAPI customOpenAPI() {
        return new OpenAPI().info(new Info().title("开发文档").description(serviceName + "模块接口").contact(new Contact().name("张仁辉").email("332120043@qq.com")).version("1.0"));
    }

    @Bean
    @ConditionalOnMissingBean(Knife4jProperties.class)
    @ConfigurationProperties(prefix = "knife4j")
    public Knife4jProperties knife4jProperties() {
        Knife4jProperties props = new Knife4jProperties();
        if (props.getSetting() == null) {
            props.setSetting(new Knife4jSetting());
        }
        if (props.getBasic() == null) {
            props.setBasic(new Knife4jHttpBasic());
        }
        props.setEnable(true);
        props.getSetting().setLanguage(OpenAPILanguageEnums.ZH_CN);
        props.getBasic().setEnable(true);
        props.getBasic().setUsername("admin");
        props.getBasic().setPassword("admin");
        return props;
    }
}
